package com.linked.pay.points.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.pay.points.bean.param.CheckPointsParam;
import com.linked.pay.points.bean.po.PointsInfoPO;
import com.linked.pay.points.mapper.IPointsInfoMapper;
import com.linked.pay.points.service.IPointsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.sql.Wrapper;
import java.util.List;

/**
 * @author :dbq
 * @date : 2023/2/27 13:29
 * @description : desc
 */
@Service
@Slf4j
public class PointsInfoServiceImpl implements IPointsInfoService {

    private final IPointsInfoMapper pointsInfoMapper;

    private final ObjectMapper mapper;

    private final TransactionTemplate transactionTemplate;

    @Autowired
    public PointsInfoServiceImpl(IPointsInfoMapper pointsInfoMapper, ObjectMapper mapper, TransactionTemplate transactionTemplate) {
        this.pointsInfoMapper = pointsInfoMapper;
        this.mapper = mapper;
        this.transactionTemplate = transactionTemplate;
    }

    @Override
    public List<PointsInfoPO> queryPointsInfoList() throws Exception {
        return pointsInfoMapper.queryPointsInfoList();
    }

    @Override
    public boolean addPoints(PointsInfoPO param) throws Exception {
        QueryWrapper<PointsInfoPO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", param.getUserId());
        wrapper.eq("if_final", 1);
        PointsInfoPO temp = pointsInfoMapper.selectOne(wrapper);
        if (temp == null) {
            param.setTotalPoints(param.getPoint());
            return pointsInfoMapper.insert(param) > 0;
        } else {
            param.setTotalPoints(param.getPoint() + temp.getTotalPoints());

            return transactionTemplate.execute(action -> {
                boolean flag = pointsInfoMapper.insert(param) > 0;
                if (!flag) {
                    log.info("保存积分失败！userId:" + param.getUserId() + "   积分名称：" + param.getPointName());
                    action.setRollbackOnly();
                    return false;
                }
                temp.setIfFinal(0);
                boolean updateFlag = pointsInfoMapper.updateById(temp) > 0;
                if (!updateFlag) {
                    log.info("修改积分失败！userId:" + temp.getUserId() + "   积分名称：" + temp.getPointName());
                    action.setRollbackOnly();
                    return false;
                }
                return true;
            });
        }
    }

    @Override
    public boolean checkPoints(CheckPointsParam param) throws Exception {
        QueryWrapper<PointsInfoPO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", param.getUserId());
        wrapper.eq("if_final", 1);
        PointsInfoPO temp = pointsInfoMapper.selectOne(wrapper);
        if (temp.getTotalPoints() >= param.getPoints()) {
            return true;
        }
        return false;
    }
}
